/*********************************** (C) COPYRIGHT ******************************
 * address: https://gitee.com/liuniansihuo/multitasks-os
 *
 * Change Logs:
 * Date			Author      	Notes
 * 2019-06-26	LiuHuo			first version
 *
*******************************************************************************/

#ifndef __bitmap_H_
#define __bitmap_H_




#define BITMAP_UNIT_SIZE 32U
#define BITMAP_UNIT_MASK 0X0000001F
#define BITMAP_UNIT_BITS 5U

#define BITMAP_MASK(nr) (1UL << ((nr) & BITMAP_UNIT_MASK))
#define BITMAP_WORD(nr) ((nr) >> BITMAP_UNIT_BITS)

/**
 ** This MACRO will declare a bitmap
 ** @param[in]  name  the name of the bitmap to declare
 ** @param[in]  bits  the bits of the bitmap
 ** @return  no return
 **/
#define BITMAP_DECLARE(name, bits) unsigned int name[((bits) + (BITMAP_UNIT_SIZE - 1U)) >> BITMAP_UNIT_BITS]

/**
 ** This function will set a bit of the bitmap
 ** @param[in]  bitmap  pointer to the bitmap
 ** @param[in]  nr      position of the bitmap to set
 **/

#define bitmap_set(bitmap, nr) bitmap[BITMAP_WORD(nr)] |= BITMAP_MASK(nr);

/**
 ** This function will clear a bit of the bitmap
 ** @param[in]  bitmap  pointer to the bitmap
 ** @param[in]  nr      position of the bitmap to clear
 **/

#define bitmap_clear(bitmap,  nr)		bitmap[BITMAP_WORD(nr)] &= (~BITMAP_MASK(nr));


unsigned char clz8(unsigned char x);
unsigned char clz16(unsigned short x);
unsigned char clz32(unsigned int x);
unsigned char clz64(unsigned long long x);

unsigned char ctz8(unsigned char x);
unsigned char ctz16(unsigned short x);
unsigned char ctz32(unsigned int x);
unsigned char ctz64(unsigned long long x);

#define CLZ8(x)	((x)&0x80)?0:\
				((x)&0x40)?1:\
				((x)&0x20)?2:\
				((x)&0x10)?3:\
				((x)&0x08)?4:\
				((x)&0x04)?5:\
				((x)&0x02)?6:\
				((x)&0x01)?7:8

#define CLZ16(x)	((x)&0x8000)?0:\
				((x)&0x4000)?1:\
				((x)&0x2000)?2:\
				((x)&0x1000)?3:\
				((x)&0x0800)?4:\
				((x)&0x0400)?5:\
				((x)&0x0200)?6:\
				((x)&0x0100)?7:\
				((x)&0x0080)?8:\
				((x)&0x0040)?9:\
				((x)&0x0020)?10:\
				((x)&0x0010)?11:\
				((x)&0x0008)?12:\
				((x)&0x0004)?13:\
				((x)&0x0002)?14:\
				((x)&0x0001)?15:16

#define CLZ32(x)	((x)&0x80000000)?0:\
				((x)&0x40000000)?1:\
				((x)&0x20000000)?2:\
				((x)&0x10000000)?3:\
				((x)&0x08000000)?4:\
				((x)&0x04000000)?5:\
				((x)&0x02000000)?6:\
				((x)&0x01000000)?7:\
				((x)&0x00800000)?8:\
				((x)&0x00400000)?9:\
				((x)&0x00200000)?10:\
				((x)&0x00100000)?11:\
				((x)&0x00080000)?12:\
				((x)&0x00040000)?13:\
				((x)&0x00020000)?14:\
				((x)&0x00010000)?15:\
				((x)&0x00008000)?16:\
				((x)&0x00004000)?17:\
				((x)&0x00002000)?18:\
				((x)&0x00001000)?19:\
				((x)&0x00000800)?20:\
				((x)&0x00000400)?21:\
				((x)&0x00000200)?22:\
				((x)&0x00000100)?23:\
				((x)&0x00000080)?24:\
				((x)&0x00000040)?25:\
				((x)&0x00000020)?26:\
				((x)&0x00000010)?27:\
				((x)&0x00000008)?28:\
				((x)&0x00000004)?29:\
				((x)&0x00000002)?30:\
				((x)&0x00000001)?31:32


#define CLZ64(x)	((x)&0x8000000000000000)?0:\
				((x)&0x4000000000000000)?1:\
				((x)&0x2000000000000000)?2:\
				((x)&0x1000000000000000)?3:\
				((x)&0x0800000000000000)?4:\
				((x)&0x0400000000000000)?5:\
				((x)&0x0200000000000000)?6:\
				((x)&0x0100000000000000)?7:\
				((x)&0x0080000000000000)?8:\
				((x)&0x0040000000000000)?9:\
				((x)&0x0020000000000000)?10:\
				((x)&0x0010000000000000)?11:\
				((x)&0x0008000000000000)?12:\
				((x)&0x0004000000000000)?13:\
				((x)&0x0002000000000000)?14:\
				((x)&0x0001000000000000)?15:\
				((x)&0x0000800000000000)?16:\
				((x)&0x0000400000000000)?17:\
				((x)&0x0000200000000000)?18:\
				((x)&0x0000100000000000)?19:\
				((x)&0x0000080000000000)?20:\
				((x)&0x0000040000000000)?21:\
				((x)&0x0000020000000000)?22:\
				((x)&0x0000010000000000)?23:\
				((x)&0x0000008000000000)?24:\
				((x)&0x0000004000000000)?25:\
				((x)&0x0000002000000000)?26:\
				((x)&0x0000001000000000)?27:\
				((x)&0x0000000800000000)?28:\
				((x)&0x0000000400000000)?29:\
				((x)&0x0000000200000000)?30:\
				((x)&0x0000000100000000)?31:\
				((x)&0x0000000080000000)?32:\
				((x)&0x0000000040000000)?33:\
				((x)&0x0000000020000000)?34:\
				((x)&0x0000000010000000)?35:\
				((x)&0x0000000008000000)?36:\
				((x)&0x0000000004000000)?37:\
				((x)&0x0000000002000000)?38:\
				((x)&0x0000000001000000)?39:\
				((x)&0x0000000000800000)?40:\
				((x)&0x0000000000400000)?41:\
				((x)&0x0000000000200000)?42:\
				((x)&0x0000000000100000)?43:\
				((x)&0x0000000000080000)?44:\
				((x)&0x0000000000040000)?45:\
				((x)&0x0000000000020000)?46:\
				((x)&0x0000000000010000)?47:\
				((x)&0x0000000000008000)?48:\
				((x)&0x0000000000004000)?49:\
				((x)&0x0000000000002000)?50:\
				((x)&0x0000000000001000)?51:\
				((x)&0x0000000000000800)?52:\
				((x)&0x0000000000000400)?53:\
				((x)&0x0000000000000200)?54:\
				((x)&0x0000000000000100)?55:\
				((x)&0x0000000000000080)?56:\
				((x)&0x0000000000000040)?57:\
				((x)&0x0000000000000020)?58:\
				((x)&0x0000000000000010)?59:\
				((x)&0x0000000000000008)?60:\
				((x)&0x0000000400000000)?61:\
				((x)&0x0000000200000000)?62:\
				((x)&0x0000000100000000)?63:64



#define CTZ8(x)	((x)&0x01)?0:\
				((x)&0x02)?1:\
				((x)&0x04)?2:\
				((x)&0x08)?3:\
				((x)&0x10)?4:\
				((x)&0x20)?5:\
				((x)&0x40)?6:\
				((x)&0x80)?7:8


#define CTZ16(x)	((x)&0x0001)?0:\
				((x)&0x0002)?1:\
				((x)&0x0004)?2:\
				((x)&0x0008)?3:\
				((x)&0x0010)?4:\
				((x)&0x0020)?5:\
				((x)&0x0040)?6:\
				((x)&0x0080)?7:\
				((x)&0x0100)?8:\
				((x)&0x0200)?9:\
				((x)&0x0400)?10:\
				((x)&0x0800)?11:\
				((x)&0x1000)?12:\
				((x)&0x2000)?13:\
				((x)&0x4000)?14:\
				((x)&0x8000)?15:16


#define CTZ32(x)	((x)&0x00000001)?0:\
				((x)&0x00000002)?1:\
				((x)&0x00000004)?2:\
				((x)&0x00000008)?3:\
				((x)&0x00000010)?4:\
				((x)&0x00000020)?5:\
				((x)&0x00000040)?6:\
				((x)&0x00000080)?7:\
				((x)&0x00000100)?8:\
				((x)&0x00000200)?9:\
				((x)&0x00000400)?10:\
				((x)&0x00000800)?11:\
				((x)&0x00001000)?12:\
				((x)&0x00002000)?13:\
				((x)&0x00004000)?14:\
				((x)&0x00008000)?15:\
				((x)&0x00010000)?16:\
				((x)&0x00020000)?17:\
				((x)&0x00040000)?18:\
				((x)&0x00080000)?19:\
				((x)&0x00100000)?20:\
				((x)&0x00200000)?21:\
				((x)&0x00400000)?22:\
				((x)&0x00800000)?23:\
				((x)&0x01000000)?24:\
				((x)&0x02000000)?25:\
				((x)&0x04000000)?26:\
				((x)&0x08000000)?27:\
				((x)&0x10000000)?28:\
				((x)&0x20000000)?29:\
				((x)&0x40000000)?30:\
				((x)&0x80000000)?31:32


#define CTZ64(x)	((x)&0x0000000000000001)?0:\
				((x)&0x0000000000000002)?1:\
				((x)&0x0000000000000004)?2:\
				((x)&0x0000000000000008)?3:\
				((x)&0x0000000000000010)?4:\
				((x)&0x0000000000000020)?5:\
				((x)&0x0000000000000040)?6:\
				((x)&0x0000000000000080)?7:\
				((x)&0x0000000000000100)?8:\
				((x)&0x0000000000000200)?9:\
				((x)&0x0000000000000400)?10:\
				((x)&0x0000000000000800)?11:\
				((x)&0x0000000000001000)?12:\
				((x)&0x0000000000002000)?13:\
				((x)&0x0000000000004000)?14:\
				((x)&0x0000000000008000)?15:\
				((x)&0x0000000000010000)?16:\
				((x)&0x0000000000020000)?17:\
				((x)&0x0000000000040000)?18:\
				((x)&0x0000000000080000)?19:\
				((x)&0x0000000000100000)?20:\
				((x)&0x0000000000200000)?21:\
				((x)&0x0000000000400000)?22:\
				((x)&0x0000000000800000)?23:\
				((x)&0x0000000001000000)?24:\
				((x)&0x0000000002000000)?25:\
				((x)&0x0000000004000000)?26:\
				((x)&0x0000000008000000)?27:\
				((x)&0x0000000010000000)?28:\
				((x)&0x0000000020000000)?29:\
				((x)&0x0000000040000000)?30:\
				((x)&0x0000000080000000)?31:\
				((x)&0x0000000100000000)?32:\
				((x)&0x0000000200000000)?33:\
				((x)&0x0000000400000000)?34:\
				((x)&0x0000000800000000)?35:\
				((x)&0x0000001000000000)?36:\
				((x)&0x0000002000000000)?37:\
				((x)&0x0000004000000000)?38:\
				((x)&0x0000008000000000)?39:\
				((x)&0x0000010000000000)?40:\
				((x)&0x0000020000000000)?41:\
				((x)&0x0000040000000000)?42:\
				((x)&0x0000080000000000)?43:\
				((x)&0x0000100000000000)?44:\
				((x)&0x0000200000000000)?45:\
				((x)&0x0000400000000000)?46:\
				((x)&0x0000800000000000)?47:\
				((x)&0x0001000000000000)?48:\
				((x)&0x0002000000000000)?49:\
				((x)&0x0004000000000000)?50:\
				((x)&0x0008000000000000)?51:\
				((x)&0x0010000000000000)?52:\
				((x)&0x0020000000000000)?53:\
				((x)&0x0040000000000000)?54:\
				((x)&0x0080000000000000)?55:\
				((x)&0x0100000000000000)?56:\
				((x)&0x0200000000000000)?57:\
				((x)&0x0400000000000000)?58:\
				((x)&0x0800000000000000)?59:\
				((x)&0x1000000000000000)?60:\
				((x)&0x2000000000000000)?61:\
				((x)&0x4000000000000000)?62:\
				((x)&0x8000000000000000)?63:64


#endif


